package x.y.t;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.ImageView;
import android.widget.TabWidget;
import android.widget.TextView;

public class MyTabHostAcitivity extends TabActivity {

	private static final String TAG = MyTabHostAcitivity.class.getSimpleName();
	private AnimationTabHost mTabHost;
	private TabWidget mTabWidget;
	private ImageView cursor;
	private int offset = 0;
	private int currIndex = 0;
	private int bmpW;
	
	private ImageView[] views; 

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_tabhost);

		InitImageView();

		mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
		mTabHost = (AnimationTabHost) findViewById(android.R.id.tabhost);
		
		new Handler().postDelayed(new Runnable() {
			public void run() {
				initBottomMenu();
			}
		}, 300);
		
		init();
	}
	
	private int getImageId(int index, boolean isSelect){
		int result = -1;
		switch (index) {
		case 0: 
			result = isSelect ? R.drawable.main_tab_home_selected : R.drawable.main_tab_home_normal;
			break;
		case 1:
			result = isSelect ? R.drawable.main_tab_category_selected : R.drawable.main_tab_category_normal;
			break;
		case 2:
			result = isSelect ? R.drawable.main_tab_recommend_selected : R.drawable.main_tab_recommend_normal;
			break;
		case 3:
			result = isSelect ? R.drawable.main_tab_rank_selected : R.drawable.main_tab_rank_normal;
			break;
		case 4:
			result = isSelect ? R.drawable.main_tab_app_selected : R.drawable.main_tab_app_normal;
			break;
		}
		return result;
	}

	private void initBottomMenu() {
		int viewCount = mTabWidget.getChildCount();
		
		views = new ImageView[viewCount];
		for (int i = 0; i < views.length; i++) {
			View v = (LinearLayout) mTabWidget.getChildAt(i);
			views[i] = (ImageView) v.findViewById(R.id.main_activity_tab_image);
		}
		
		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
			public void onTabChanged(String tabId) {

				int tabID = Integer.valueOf(tabId);
				views[currIndex].setImageResource(getImageId(currIndex, false));
				views[tabID].setImageResource(getImageId(tabID, true));
				onPageSelected(tabID);
			}
		});
	}

	private void init() {

		setIndicator("首页", 0, new Intent(this, FirstPage.class), R.drawable.main_tab_home_selected);
		setIndicator("分类", 1, new Intent(this, SecondPage.class), R.drawable.main_tab_category_normal);
		setIndicator("达人", 2, new Intent(this, ThirdPage.class), R.drawable.main_tab_recommend_normal);
		setIndicator("排行", 3, new Intent(this, FourthPage.class), R.drawable.main_tab_rank_normal);
		setIndicator("管理", 4, new Intent(this, FifthPage.class), R.drawable.main_tab_app_normal);
		mTabHost.setOpenAnimation(true);
	}

	private void setIndicator(String ss, int tabId, Intent intent, int image_id) {

		View localView = LayoutInflater.from(this.mTabHost.getContext()).inflate(R.layout.tab_widget_view, null);
		((TextView) localView.findViewById(R.id.main_activity_tab_text)).setText(Html.fromHtml("<font color='white' >" + ss + "</font>"));
		((ImageView) localView.findViewById(R.id.main_activity_tab_image)).setImageResource(image_id);
		String str = String.valueOf(tabId);
		TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(str).setIndicator(localView).setContent(intent);
		mTabHost.addTab(localTabSpec);
	}

	private void InitImageView() {

		cursor = (ImageView) findViewById(R.id.cursor);
		bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.main_tab_anim_light).getWidth();
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		offset = (screenW / 5 - bmpW) / 2;
		Log.i(TAG, "("+screenW+"/5-"+bmpW+")/2="+offset);
		Matrix matrix = new Matrix();
		matrix.postTranslate(offset, 0);
		cursor.setImageMatrix(matrix);
	}

	public void onPageSelected(int arg0) {
		
		int one = offset * 2 + bmpW;
		Log.i(TAG, "one ="+one );
		Animation animation = null;
		
		animation = new TranslateAnimation(one * currIndex, one * arg0 , 0, 0);
		
		currIndex = arg0;
		
		animation.setFillAfter(true);
		animation.setDuration(300);
		cursor.startAnimation(animation);
	}

}